package com.sanpu.kxe.business.repay.presenter;

import com.sanpu.kxe.business.repay.bean.QueryPlanDetailsOutput;
import com.sanpu.kxe.business.repay.contract.QueryPlanDetailsContract;
import com.sanpu.kxe.business.repay.model.RepayModel;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：查询计划详情
 * Created by 薛勇军 on 2019/4/10.
 */

public class QueryPlanDetailsPresenter implements QueryPlanDetailsContract.Presenter {

    private QueryPlanDetailsContract.View mView;

    private CompositeSubscription mSubscription;

    public QueryPlanDetailsPresenter(QueryPlanDetailsContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void cardPlanDetailList(String repayOrderNo) {
        mView.showLoadingDialog(false);
        Subscription subscription = RepayModel.getInstance().cardPlanDetailList(repayOrderNo)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<QueryPlanDetailsOutput>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(QueryPlanDetailsOutput queryPlanDetailsOutput) {
                        mView.showDataView(queryPlanDetailsOutput);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void cancelPlan(String repayOrderNo) {
        mView.showLoadingDialog(true);
        Subscription subscription = RepayModel.getInstance().cancelPlan(repayOrderNo)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.showCancelView();
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
